class MiscController < ApplicationController
  caches_page :air_quality

  TITLE_FORMAT = 
    /BeijingAir: (\d+-\d+-\d+); (\d+:\d+); PM2.5; (\d+\.\d+); (\d+);/

  def air_quality
    return render(:text => "Not supported on localhost") if request.host == "localhost"
    
    begin
      response = Net::HTTP.get_response(URI.parse(
        %Q(http://twitter.com/statuses/user_timeline/15527964.rss) ))
        
      if Net::HTTPOK === response
        begin
          h = Hash.from_xml(response.body)
          items = h["rss"]["channel"]["item"]
          
          items.each do |item|
            if item["title"] =~ TITLE_FORMAT
              date = $1
              time = $2
              aqi = $4
              
              return render(
                :text => "%s / 500 [ %s - %s ]" % [aqi, date, time])
            end
          end
        rescue
        end
      end
    rescue
    end

    render( :text => "Air quality not available right now." )
  end
  
  def expire_air_quality
    expire_page "/misc/air_quality"
  end
end